/**
 * Created by 张晨 on 2018/11/28.
 */
window.onload = function () {
    $("#mask").fadeOut();
    var nav_sidebar=$.ajax({
        url:"/sidebar",
        type:"GET",
        async:false
    });
    $("#sidebar").html(nav_sidebar.responseText);
};

$("#logout").click(function(){
    $.get("/logout");
    $("html").fadeOut();
    setTimeout("window.location.reload();",500);
});

var getAnswerpage=function(e){
    var Answerpage=$.ajax({
        url:"/answerpage",
        type:"GET",
        data:{
            question_id:e.id
        },
        async:false
    });
    $("#main-content").html(Answerpage.responseText);
};

var test = function(args1){
    console.log(args1);
}

var getClassTotal = function(){
    var classpage=$.ajax({
        url:"/classTotal",
        type:"GET",
        async:false
    });
    $("#main-content").html(classpage.responseText);
}

var getClassManagementpage=function (class_id) {
    var result=$.ajax({
        url:"/classManagement",
        type:"GET",
        data:{
            class_id: class_id
        },
        async:false
    });
    $("#main-content").html(result.responseText);
};

var removeClassStudent=function (class_id,student_id) {
    var result=$.ajax({
        url:"/removeClassStudent",
        type:"GET",
        data:{
            class_id: class_id,
            student_id: student_id
        },
        async:false
    });
    alert(result.responseText);
    getClassManagementpage(class_id);
};

var addStudentManualPage = function(){
    var result=$.ajax({
        url:"/addStudentManualPage",
        type:"GET",
        async:false
    });
    $("#main-content").html(result.responseText);
}

var addStudentManual = function(){
    var result=$.ajax({
        url:"/addStudentManual",
        type:"POST",
        data:{
            number:$("#number").val(),
            name:$("#name").val(),
            class_name:$("#class_name").val()
        },
        async:false
    });
    alert(result.responseText);
    getStudentManagementPage();
}

var getStudentManagementPage=function (e) {
    var studentpage=$.ajax({
        url:"/studentManagement",
        type:"GET",
        async:false
    });
    $("#main-content").html(studentpage.responseText);
};


var getQuestionpage=function () {
    var questionpage=$.ajax({
        url:"/questions",
        type:"GET",
        async:false
    });
    $("#main-content").html(questionpage.responseText);
};



var getQuestionTotal=function () {
    var questionpage=$.ajax({
        url:"/questionsTotal",
        type:"GET",
        async:false
    });
    $("#main-content").html(questionpage.responseText);
};


var getAddQuestionPage=function () {
    var addquestionpage=$.ajax({
        url:"/addquestion",
        type:"GET",
        async:false
    });
    $("#main-content").html(addquestionpage.responseText);
};

var addQuestion=function () {
    var add_result=$.ajax({
        url:"/question/add",
        type:"POST",
        data:{
            title:$("#title").val(),
            description:$("#description").val()
        },
        async:false
    });
    alert(add_result.responseText);
    getQuestionpage();
}

var deleteQuestion=function (e) {
    var delete_result=$.ajax({
        url:"/question/delete",
        type:"POST",
        data:{
            question_id:e.id //e.id通过下面的alert测试证实不为空
        },
        async:false
    });
    alert(delete_result.responseText);
    getQuestionpage();
}

var getEditQuestionpage=function (e) {
    var editquestionpage=$.ajax({
        url:"/editquestion",
        type:"GET",
        data:{
          question_id:e.id
        },
        async:false
    });
    $("#main-content").html(editquestionpage.responseText);
};

var editQuestion=function (e) {
    var edit_result=$.ajax({
        url:"/question/edit",
        type:"POST",
        data:{
            question_id:e.id,
            title:$("#title").val(),
            description:$("#description").val()
        },
        async:false
    });
    alert(edit_result.responseText);
    getQuestionpage();
};

var getListspage=function () {
    var listspage=$.ajax({
        url:"/lists",
        type:"GET",
        async:false
    });
    $("#main-content").html(listspage.responseText);
};

var getAddListpage=function () {
    var addlistpage=$.ajax({
        url:"/addlist",
        type:"GET",
        async:false
    });
    $("#main-content").html(addlistpage.responseText);
};

var addList=function () {
    //获取复选框题目列表
    var question_list=new Array();
    $("input[name='question_id']:checked").each(function(){
        question_list.push($(this).val());//向数组中添加元素
    });

    var add_result=$.ajax({
        url:"/list/add",
        type:"POST",
        data:{
            list_name:$("#list_name").val(),
            description:$("#description").val(),
            class_id:$("#class_id").val(),
            question_id_list:question_list.join()
        },
        async:false
    });
    alert(add_result.responseText);
    getListspage();
};

var deleteList=function (e) {
    var delete_result=$.ajax({
        url:"/list/delete",
        type:"POST",
        data:{
            list_question_id:e.id
        },
        async:false
    });
    alert(delete_result.responseText);
    getListspage()
};
var getEditClasspage =  function getEditClasspage(e) {
    var editquestionpage=$.ajax({
        url:"/editClassInfo",
        type:"GET",
        data:{
            class_id:e.id
        },
        async:false
    });
    $("#main-content").html(editquestionpage.responseText);
}

var editClass =  function editClass(e) {
    var editClassMsg=$.ajax({
        url:"/editClass",
        type:"GET",
        data:{
            class_id:e.id,
            teacher_id:$("#teacher_id").val(),
            class_name:$("#class_name").val()
        },
        async:false
    });
    if(editClassMsg.responseText=='successful'){
        alert("修改成功")
        var listpage=$.ajax({
            url:'/classes?class_id='+e.id,
            type:"GET",
            async:false
        });
        $("#main-content").html(listpage.responseText);
        return
    }else{
        alert("修改失败")
    }
}

$(function () {
    $("#changeEditPage").bind("click",function (event) {
        event.preventDefault();//导入的3.2.0版本 把人都害死
        $(".showPage").hide();
        $(".editPage").show();
    })
})

var editStudent = function (e) {
    var editStudentMsg=$.ajax({
        url:"/editPersonalinfo",
        type:"POST",
        data:{
            student_id:e.id,
            student_name:$("#student_name").val()
        },
        async:false
    });
    if(editStudentMsg.responseText=='seccussful'){
        alert("修改成功")
        location.href = "/"
    }else{
        alert("修改失败")
    }
}
var readList = function (e) {
    var rlist=$.ajax({
        url:"/readRequestion",
        type:"GET",
        async:false,
        data:{
            question_id:e.id
        }
    });
    $("#main-content").html(rlist.responseText);
}
// function stopBubble(e){
//     if (e && e.stopPropagation) {
//         e.stopPropagation();
//     } else {
//         e.cancelBubble = true;
//     }
//     event.stopImmediatePropagation();
// }

function uploadStudentFile(){
    var formData = new FormData();
    var name = $("#studentFile").val();
    var classId = $("#studentFileClass").val();
    console.log(name);
    formData.append("file",$("#studentFile")[0].files[0]);
    formData.append("name",name);
    formData.append("classId",classId);
    $.ajax({
        url : '/uploadStudentFile',
        type : 'POST',
        async : false,
        data : formData,
        // 告诉jQuery不要去处理发送的数据
        processData : false,
        // 告诉jQuery不要去设置Content-Type请求头
        contentType : false,
        success : function(responseStr) {
            if(responseStr=="successful"){
                alert("导入成功");
            }else{
                alert(responseStr);
            }
        }
    });
    $('#fileUpload').modal('hide');
    getClassTotal();
}

var addClassPage = function(){
    var result=$.ajax({
        url:"/addClassPage",
        type:"GET",
        async:false
    });
    $("#main-content").html(result.responseText);
}

var addClass = function(){
    var result=$.ajax({
        url:"/addClass",
        type:"POST",
        data:{
            number:$("#number").val(),
            name:$("#name").val(),
            teacherId:$("#teacherId").val(),
            max : $("#max").val()
        },
        async:false
    });
    alert(result.responseText);
    getClassTotal();
}

var selectStudentClass = function(classId){
    var result=$.ajax({
        url:"/selectStudentClass",
        type:"GET",
        data:{
            classId : classId
        },
        async:false
    });
    $("#main-content").html(result.responseText);
}

function saveStudentClass(classId){
    var chk_value =[];//定义一个数组
    $('input[name="studentClassCheckbox"]:checked').each(function(){
        chk_value.push($(this).val().toString());//将选中的值添加到数组chk_value中
    });
    var result=$.ajax({
        url:"/saveStudentClass",
        type:"POST",
        data:{
            classId : classId.toString(),
            values : chk_value
        },
        async:false
    });
    alert(result.responseText);
    getClassTotal();
}

var getChoices=function (questionId) {
    var result=$.ajax({
        url:"/getChoices",
        type:"GET",
        data:{
          questionId:questionId
        },
        async:false
    });
    $("#main-content").html(result.responseText);
};

var removeChoice = function(choiceId,questionId){
    var result=$.ajax({
        url:"/removeChoice",
        type:"GET",
        data:{
            choiceId: choiceId
        },
        async:false
    });
    alert(result.responseText);
    getChoices(questionId);
}

var addChoicePage=function (questionId) {
    var result=$.ajax({
        url:"/addChoicePage",
        type:"GET",
        data:{
          questionId:questionId
        },
        async:false
    });
    $("#main-content").html(result.responseText);
};

var addChoice = function(questionId){
    var result=$.ajax({
        url:"/addChoice",
        type:"POST",
        data:{
            choiceInfo:$("#choiceInfo").val(),
            questionId: questionId,
            op1:$("#op1").val(),
            op2:$("#op2").val(),
            op3:$("#op3").val(),
            op4:$("#op4").val()
        },
        async:false
    });
    alert(result.responseText);
    getChoices(questionId);
}





var getDesigns=function (questionId) {
    var result=$.ajax({
        url:"/getDesigns",
        type:"GET",
        data:{
          questionId:questionId
        },
        async:false
    });
    $("#main-content").html(result.responseText);
};

var removeDesign = function(designId,questionId){
    var result=$.ajax({
        url:"/removeDesign",
        type:"GET",
        data:{
            designId: designId
        },
        async:false
    });
    alert(result.responseText);
    getDesigns(questionId);
}

var addDesignPage=function (questionId) {
    var result=$.ajax({
        url:"/addDesignPage",
        type:"GET",
        data:{
          questionId:questionId
        },
        async:false
    });
    $("#main-content").html(result.responseText);
};

var addDesign = function(questionId){
    var result=$.ajax({
        url:"/addDesign",
        type:"POST",
        data:{
            designInfo:$("#designInfo").val(),
            questionId: questionId
        },
        async:false
    });
    alert(result.responseText);
    getDesigns(questionId);
}

var getPublishPage = function(e){
    var result=$.ajax({
        url:"/getPublishPage",
        type:"GET",
        data:{
          questionId:e.id
        },
        async:false
    });
    $("#main-content").html(result.responseText);
}

var publishHomework = function(questionId){
    var chk_value =[];//定义一个数组
    $('input[name="publishCheckbox"]:checked').each(function(){
        chk_value.push($(this).val().toString());//将选中的值添加到数组chk_value中
    });
    var result=$.ajax({
        url:"/publishHomework",
        type:"POST",
        data:{
            name:$("#homeWorkName").val(),
            publishTime: $("#publishTime").val(),
            endTime: $("#endTime").val(),
            questionId: questionId,
            classIds: chk_value
        },
        async:false
    });
    alert(result.responseText);
    getQuestionpage();
}

var getNotificationTotal = function(){
var result=$.ajax({
        url:"/notification/total",
        type:"GET",
        async:false
    });
    $("#main-content").html(result.responseText);
}

var addNotificationPage = function(){
var result=$.ajax({
        url:"/notification/teacher/addPage",
        type:"GET",
        async:false
    });
    $("#main-content").html(result.responseText);
}

var addNotification = function(){
    var chk_value =[];//定义一个数组
    $('input[name="notificationCheckbox"]:checked').each(function(){
        chk_value.push($(this).val().toString());//将选中的值添加到数组chk_value中
    });
    var result=$.ajax({
        url:"/notification/teacher/add",
        type:"POST",
        data:{
            info:$("#notificationInfo").val(),
            level: $("#notificationLevel").val(),
            classIds: chk_value
        },
        async:false
    });
    alert(result.responseText);
    getNotificationTotal();
}

var removeNotification = function(notificationId){
    var result=$.ajax({
        url:"/notification/teacher/remove",
        type:"POST",
        data:{
            notificationId: notificationId
        },
        async:false
    });
    alert(result.responseText);
    getNotificationTotal();
}






var getExamTotal = function(){
var result=$.ajax({
        url:"/exam/total",
        type:"GET",
        async:false
    });
    $("#main-content").html(result.responseText);
}

var addExamPage = function(){
var result=$.ajax({
        url:"/exam/teacher/addPage",
        type:"GET",
        async:false
    });
    $("#main-content").html(result.responseText);
}

var addExam = function(){
    var chk_value =[];//定义一个数组
    $('input[name="examCheckbox"]:checked').each(function(){
        chk_value.push($(this).val().toString());//将选中的值添加到数组chk_value中
    });
    var result=$.ajax({
        url:"/exam/teacher/add",
        type:"POST",
        data:{
            name:$("#examName").val(),
            examTime: $("#examTime").val(),
            classIds: chk_value
        },
        async:false
    });
    alert(result.responseText);
    getExamTotal();
}

var removeExam = function(examId){
    var result=$.ajax({
        url:"/exam/teacher/remove",
        type:"POST",
        data:{
            examId: examId
        },
        async:false
    });
    alert(result.responseText);
    getExamTotal();
}

var getSubmits = function(questionId){
    var result=$.ajax({
        url:"/teacher/homework/getSubmits",
        type:"GET",
        data:{
            questionId:questionId
        },
        async:false
    });
    $("#main-content").html(result.responseText);
}



var removeStudent = function(studentId){
    console.log(studentId);
    var result=$.ajax({
        url:"/studentRemove",
        type:"GET",
        data:{
            studentId:studentId
        },
        async:false
    });
    alert(result.responseText);
    getStudentManagementPage();
}



function updateModelValue(classId){
    $("#studentFileClass").val(classId);
}


function readDocumentBySubmit(submitId){
    $.ajax({
        url: "/teacher/homework/download",
        type:"GET",
        async : false,
        data : {
            submitId : submitId
        },
        success : function(responseStr) {
            var option = {
                docId: responseStr.docId,
                token: 'TOKEN',
                host: 'BCEDOC',
                serverHost: 'http://doc.bj.baidubce.com',
                width: 600, //文档容器宽度
                zoom: false,              //是否显示放大缩小按钮
                zoomStepWidth:200,
                ready: function (handler) {  // 设置字体大小和颜色, 背景颜色（可设置白天黑夜模式）
                    handler.setFontSize(1);
                    handler.setBackgroundColor('#000');
                    handler.setFontColor('#fff');
                },
                flip: function (data) {    // 翻页时回调函数, 可供客户进行统计等
                    console.log(data.pn);
                },
                fontSize:'big',
                toolbarConf: {
                        page: true, //上下翻页箭头图标
                        pagenum: true, //几分之几页
                        full: false, //是否显示全屏图标,点击后全屏
                        copy: true, //是否可以复制文档内容
                        position: 'center',// 设置 toolbar中翻页和放大图标的位置(值有left/center)
                } //文档顶部工具条配置对象,必选
            };
            new Document('teacherDocContent', option);
        }
    });
}